<?php
    class Admin_Model_Role extends Zend_Db_Table_Abstract
    {
        protected $_name = 'role';
        protected $_primary = 'role_id';
        protected $_db;
        
        public function __construct(){
            $this->_db = Zend_Registry::get('db');
        }
        
        public function listRole(){
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->order('role_id DESC');
            return $sql;
        }
        
        public function getRoleById($role_id){
            $where = $this->getAdapter()->quoteInto('role_id = ?', $role_id);
            $sql = $this->_db->select()
                                ->from($this->_name)
                                ->where($where);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function checkRole($role_name, $role_id = ''){
			if($role_id != ''){
				$sql = $this->_db->select()
								->from($this->_name)
								->where('role_name = ?', $role_name)
                                ->where('role_id != ?', $role_id);
				$result = $this->_db->fetchAll($sql);
				return count($result);
			}
			else{
				$sql = $this->_db->select()
								->from($this->_name)
								->where('role_name = ?', $role_name);
				$result = $this->_db->fetchAll($sql);
				return count($result);
			}
		}
        
        public function addRole($data){
            $sql = $this->_db->insert($this->_name, $data);
            $role_id = $this->_db->lastInsertId($this->_name, $this->_primary);
            return $role_id;
        }
        
        public function editRole($data, $role_id){
            $where = $this->getAdapter()->quoteInto('role_id = ?', $role_id);
            $this->_db->update($this->_name, $data, $where);
        }
        
        public function deleteRole($role_id){
            $where = $this->getAdapter()->quoteInto('role_id = ?', $role_id);
            $this->_db->delete($this->_name, $where);
            $index = Zend_Search_Lucene::open('files/role');
            $hits = $index->find('role_id:' . $role_id);           
            foreach($hits as $hit){
                $index->delete($hit->id);
            }
            $index->commit();
            $index->optimize();
        }
        
        public function deleteRoles($listId){
            $index = Zend_Search_Lucene::open('files/role');
            foreach ($listId as $value){
                $where = $this->getAdapter()->quoteInto('role_id = ?', $value);
                $this->_db->delete($this->_name, $where);
                $hits = $index->find('role_id:' . $value);
                foreach($hits as $hit){
                    $index->delete($hit->id);
                }
			}
            $index->commit();
            $index->optimize();
        }
        
        public function searchRole($keyword){
            Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8');
            setlocale(LC_CTYPE, 'UTF-8');
            Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
			$index = Zend_Search_Lucene::open('files/role');
            if(!empty($keyword)){
                $hits = $index->find($keyword);
                return $hits;
            }
		}
        
        public function saveAddItem($arrParam = null, $options = null){
            if($options['task'] == 'add'){
                try{
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::open('files/role');
                    Zend_Search_Lucene::setResultSetLimit(350);
                }
                catch(exception $e){
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::create('files/role');
                }
                
                $doc = new Zend_Search_Lucene_Document();
                $doc->addField(Zend_Search_Lucene_Field::keyword('role_id', $arrParam['role_id']))
                    ->addField(Zend_Search_Lucene_Field::text('role_name', $arrParam['role_name'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('role_name_filter', $arrParam['role_name_filter'], 'UTF-8'));
                    
                $index->addDocument($doc);
                $index->commit();
                $index->optimize();
            }
            
            if($options['task'] == 'edit'){
                try{
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::open('files/role');
                    Zend_Search_Lucene::setResultSetLimit(350);
                }
                catch(exception $e){
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::create('files/role');
                }
                
                $hits = $index->find('role_id:'.$arrParam['role_id']);
                foreach($hits as $hit){
                    $index->delete($hit->id);
                }
                
                $doc = new Zend_Search_Lucene_Document();
                $doc->addField(Zend_Search_Lucene_Field::keyword('role_id', $arrParam['role_id']))
                    ->addField(Zend_Search_Lucene_Field::text('role_name', $arrParam['role_name'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('role_name_filter', $arrParam['role_name_filter'], 'UTF-8'));
                
                $index->addDocument($doc);
                $index->commit();
                $index->optimize();
            }
        }
    }
